package vn.allmobile.ambnews.activities.list;



import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.aphidmobile.flip.FlipViewController;
import vn.allmobile.ambnews.AboutActivity;
import vn.allmobile.ambnews.BaseActivity;
import vn.allmobile.ambnews.R;
import vn.allmobile.ambnews.SettingsActivity;
import vn.allmobile.ambnews.activities.main.HomeActivity;
import vn.allmobile.ambnews.activities.main.MainAllPapersActivity;
import vn.allmobile.ambnews.customview.FlipViewInterface;
import vn.allmobile.ambnews.customview.LoadingDialog;
import vn.allmobile.ambnews.flipview.FlipView;
import vn.allmobile.ambnews.flipview.OverFlipMode;
import vn.allmobile.ambnews.network.IncommingRequestListenter;
import vn.allmobile.ambnews.network.LoadDataListener;
import vn.allmobile.ambnews.network.NetworkAPI;
import vn.allmobile.ambnews.network.NetworkChangeReceiver;
import vn.allmobile.ambnews.utils.Contants;
import vn.allmobile.ambnews.utils.Log;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;
import android.content.Intent;

public class ListActivity extends BaseActivity implements LoadDataListener    {
	ListFlipViewAdapter adapter;
	int _screenWidth ;
	int _screenHeight;
	String list_type;
	LoadingDialog _dialog;
	//FlipViewController flipController;
	FlipViewInterface flipview;
	public Handler mHandle = new Handler()
	{
	  public void handleMessage(Message paramAnonymousMessage)
	  {
		  switch (paramAnonymousMessage.what)
	    {
	    	case 0:
	    		//success
	    		//pbarDialog.dismiss();
	    		//_dialog.dismiss();
	    		_dialog.hide();
	    		adapter  = new ListFlipViewAdapter(ListActivity.this,list_type);
				adapter._flipview  = flipview;
				flipview.setAdapter(adapter);
			
	    		break;
	    	case 1:
	    		_dialog.hide();
	    		try{
	    			Toast.makeText(ListActivity.this, "Không load được dữ liệu", Toast.LENGTH_SHORT).show();
	    			if(NetworkAPI._listArticles.size() == 0)
		    		  {
		    			  ListActivity.this.onBackPressed();
		    		  }
	    		}catch(Exception e){
	    			ListActivity.this.onBackPressed();
	    		}
	    					    	
	    		break;
	    	case 2:
	    		//on fail
	    		_dialog.hide();	    			    	
	    		
	    	break;	
	    }
	  }
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);	
		
		Intent intent = getIntent();
	
		list_type = intent.getStringExtra(Contants.EXTRA_LIST_TYPE);
		
		Log.v("list_type:" + list_type);
		
		try{
			flipview = (FlipView) findViewById(R.id.flipViewList);
			flipview.setOverFlipMode(OverFlipMode.RUBBER_BAND);
		}catch(Exception e)
		{
			flipview = (FlipViewController) findViewById(R.id.flipViewList);
		}
		if(list_type != null  && list_type.equalsIgnoreCase(Contants.LIST_TYPE_HOT) )
		{ 
			adapter  = new ListFlipViewAdapter(this,list_type);
			adapter._flipview  = flipview;
			flipview.setAdapter(adapter);
		}
		else
		{
			
			try{
				if(NetworkAPI._listArticles.size() > 0)
				{
					adapter  = new ListFlipViewAdapter(this,list_type);
					adapter._flipview  = flipview;
					flipview.setAdapter(adapter);
				}
					
			}catch(Exception e){
				
			}
			
			
			if(NetworkChangeReceiver.isConnected(true))
			{
				_dialog  = new LoadingDialog(this,mHandle,1);
				_dialog.show();
				NetworkAPI._()._loaderThread.setloadDataListener(this);			
				NetworkAPI.onSetRequest(Contants.MSG_LOAD_LIST);
			}
					
		}
		
	}

/*	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.main,  menu);
		return super.onCreateOptionsMenu(menu);
		
	}


	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
			case R.id.menu_settings:
				Intent mIntent_setting = new Intent(getApplicationContext(), SettingsActivity.class);			
				startActivity(mIntent_setting);
			return true;
			case R.id.menu_paper_manage:
				 Intent mIntent = new Intent(getApplicationContext(), MainAllPapersActivity.class);			
				 startActivity(mIntent);
				return true;
			case R.id.menu_exits:
				android.os.Process.killProcess(android.os.Process.myPid());
				return true;
			case R.id.menu_about:
				 Intent intent = new Intent(getApplicationContext(), AboutActivity.class);			
				 startActivity(intent);
			default :
				return super.onMenuItemSelected(featureId, item);
		}
		
	}
*/	
	@Override
	  protected void onResume() {
	    super.onResume();
	    //flipController.onResume();
	  }

	  @Override
	  protected void onPause() {
	    super.onPause();
	    //flipController.onPause();
	  }
	  
	  @Override
		public void onBackPressed() {
			if(NetworkAPI._fromNotification)
			{
				Intent intent = new Intent(this,HomeActivity.class);
				startActivity(intent);
				finish();
				NetworkAPI._fromNotification = false;
			}
			else
			{
				super.onBackPressed();
			}
			
			
		}

	@Override
	public void onUserloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onPaperloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onCategoryloadSuccess() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onListloadSuccess() {
		// TODO Auto-generated method stub
		this.mHandle.sendEmptyMessage(0);	
	}

	@Override
	public void onListloadFail() {
		// TODO Auto-generated method stub
		this.mHandle.sendEmptyMessage(1);
	}

	
}
